package com.supermap.dataservice.cloud.warnInfo;

import com.alibaba.dubbo.config.annotation.Service;
import com.supermap.dataservice.mysql.config.DynamicDataSourceContextHolder;
import com.supermap.dataservice.util.JSONUtil;
import com.supermap.dataservice.util.JSONUtils;
import com.supermap.dubbo.api.cloud.warnInfo.WarnInfo;
import com.supermap.dubbo.api.cloud.warnInfo.WarningInfoService;
import org.apache.logging.log4j.util.Strings;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author aqua
 * @description
 * @date 2018年12月03 下午 4:50
 * @email 1301584033@qq.com
 */
@Service
public class WarningInfoServiceImpl implements WarningInfoService {

    @Resource
    private WarningInfoMapper warningInfoMapper;
    @Override
    public void insertWarnInfoList(List<WarnInfo> warnInfoList) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        warningInfoMapper.insertWarnInfoList(warnInfoList);
    }

    @Override
    public JSONUtil getWarnInfoById(Integer warnInfoId) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        return new JSONUtil().setData(warningInfoMapper.getWarnInfoById(warnInfoId));
    }

    @Override
    public JSONUtil getPageWarnInfoByTimeOrareaCode(String areaCode,String aging, String beginTime, String endTime,String eType, Integer pageNum, Integer pageSize) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        if(Strings.isBlank(areaCode) || pageNum == null ||  pageSize==null)
        {
            return  new JSONUtil().setMsg("获取指定时间、区域、预警类型内的预警信息列表【getPageWarnInfoByTimeOrareaCode】参数错误:【areaCode】=" +
                    areaCode+ " 请确认参数！");
        }

        return new JSONUtil().setData(warningInfoMapper.
                getPageWarnInfoByTimeOrAreaCode(areaCode,aging,beginTime,endTime,eType,pageNum,pageSize))
                .setCountSum(warningInfoMapper.getPageWarnInfoByTimeOrAreaCodeCountNum(areaCode,aging,beginTime,endTime,eType,pageNum,pageSize));
    }

    @Override
    public JSONUtil getWarnTypeByTimeOrAreaCode(String areaCode, String beginTime, String endTime) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        if(Strings.isBlank(areaCode))
        {
            return  new JSONUtil().setMsg("获取指定时间和区域内的预警信息类型【getWarnTypeByTimeOrAreaCode】参数错误:【areaCode】=" +
                    areaCode+ " 请确认参数！");
        }
        return new JSONUtil().setData(warningInfoMapper.getWarnTypeByTimeOrAreaCode(areaCode,beginTime,endTime));
    }

    @Override
    public JSONUtils selectValidListByCode(String areaCode) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        List<Map<String , Object>> warningList = warningInfoMapper.selectValidListByCode(areaCode);
        return new JSONUtils().setData(warningList).setCountSum(warningList == null ? 0 : warningList.size());
    }



}
